--[[
FilePath: \Lua\View\UIHeroManor\UIHeroManorInfo.lua
城市-加成预览
--]]

require 'View/UIBase/UIBase'

UIHeroManorInfo = UIBase.New(PanelNames.UIHeroManorInfo)
local _M = UIHeroManorInfo

_M.hasInit = false

function _M.Init(cityId)
    _M.cityId = cityId or 1;
    UIBase.Init(_M)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("BackButton"), self.OnBack);
        self.title = self:GetChild("title");
        self.list = self:GetChild("list");
        self.nothingCtrl = self:GetController("nothing");
        --self:SetDynamicList(self.list, self.ItemRender);
        _M.hasInit = true

        self:addEvent()
    end
end

function _M:addEvent()

end

function _M:removeEvent()

end

-- 显示
function _M:OnEnable()
    self.title.text = getLanguage("HeroManorText03");
    self:RefreshView();
end

function _M:RefreshView()
    local data = logicMgr.HeroManorManager.getManorInfo(self.cityId);
    self.list:RemoveChildrenToPool();
    local showNum = 0;
    for k, v in pairs(data) do
        if v.masterId ~= nil and tonumber(v.masterId) ~= 0 then
            showNum = showNum + 1;
            local item = self.list:AddItemFromPool();
            self:ItemRender(v, item);
        end
    end
    if showNum == 0 then
        self.nothingCtrl.selectedIndex = 1;
    else
        self.nothingCtrl.selectedIndex = 0;
    end

end

function _M:ItemRender(data, go)
    if data.masterId == nil or tonumber(data.masterId) == 0 then
        go.visible = false;
        return;
    end
    local name = go:GetChild("heroManorName");
    local listAttr = go:GetChild("listAttr");
    local listHero = go:GetChild("listHero");
    local icon = go:GetChild("hero"):GetChild("icon");
    local noCtrl = go:GetController("nothing");
    local attrTips = go:GetChild("tips2");
    noCtrl.selectedIndex = 0;
    listHero:RemoveChildrenToPool();
    listAttr:RemoveChildrenToPool();
    local heroInfo = logicMgr.HeroManager.getHeroDatabyPartnerId(data.masterId, true);
    if heroInfo ~= nil then
        local heroModelId = heroInfo["partnerModelId"] or heroInfo["f_HeroID"] or 0;
        local heroUseSkinId = logicMgr.SkinManager.getHeroSkinId(heroModelId)
        if heroUseSkinId and tonumber(heroUseSkinId) > 0 then
            icon.url = "ui://HalfBodyAtlas/" ..logicMgr.SkinManager.GetHeroBIconBySkinId(heroUseSkinId);
        else
            icon.url = "ui://HalfBodyAtlas/" .. heroInfo["halfIcon"];
        end
        name.text = string.format(getLanguage("HeroManorText06"), heroInfo.name);
        local visitors = logicMgr.HeroManorManager.getManorVisitors(data.masterId) or {};
        if #visitors == 0 then
            noCtrl.selectedIndex = 1;
        end
        for k = 1, #visitors do
            local item = listHero:AddItemFromPool();
            local heroData = logicMgr.HeroManager.getHeroDatabyPartnerId(visitors[k], true);
            if heroData ~= nil then
                local modelId = heroData["partnerModelId"] or heroData["f_HeroID"] or 0;
                local headIcon = item:GetChild("head"):GetChild("HeadIcon");
                logicMgr.HeroManorManager.setHeadIcon(headIcon, modelId, heroData.icon);
            end
        end
        local attrItems = logicMgr.HeroManorManager.getAllAttrByGuest(visitors, data.masterId)
        if attrItems == nil or table.nums(attrItems) < 1 then
            if noCtrl.selectedIndex == 0 then
                noCtrl.selectedIndex = 2;
                local starCfg = logicMgr.HeroManorManager.getHeroManorData(heroModelId, 20, true);
                if starCfg == nil then
                    attrTips.text = "";
                else
                    attrTips.text = string.format(getLanguage("HeroManorText20"), EnumConst.StarType[starCfg[1].f_HeroStar / 10]);
                end
            end
        else
            for k, v in pairs(attrItems) do
                local item = listAttr:AddItemFromPool();
                logicMgr.HeroManorManager.setAttrItemInfo(item, k, v);
            end
        end
    else
        name.text = "";
    end
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    self.hasInit = false
end

return _M;